package com.viewplug.view.scroll;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Scroller;
import android.widget.TextView;

import com.viewplug.R;

/**
 * Created by mac_wk on 14-12-22.
 */
public class WSrollViewHeader extends LinearLayout {
    private View mContainer;
    private boolean first_flag = false;
    private Scroller mScroller;
    private int header_height;
    private ProgressBar pro_icon;
    private ImageView img_icon;
    private final int ROTATE_ANIM_DURATION = 180;
    private Animation mRotateUpAnim;
    private Animation mRotateDownAnim;
    private TextView header_hint;
    public int getHeader_height(){return this.header_height;}
    public WSrollViewHeader(Context context) {
        this(context, null);
    }

    public WSrollViewHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    @Override
    public void computeScroll()
    {
        if (mScroller.computeScrollOffset())
        {
            setVisiableHeight(mScroller.getCurrY());
            postInvalidate();
        }
        super.computeScroll();
    }
    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_plug_listview_header,this);
        //默认高度为0
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,0);
        mContainer = findViewById(R.id.viewplug_listview_header_content);
        mScroller = new Scroller(getContext());
        pro_icon = (ProgressBar) findViewById(R.id.viewplug_listview_header_progressbar);
        img_icon = (ImageView) findViewById(R.id.viewplug_listview_header_arrow);
        header_hint = (TextView) findViewById(R.id.viewplug_listview_header_hint_textview);
        mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateUpAnim.setFillAfter(true);
        mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateDownAnim.setFillAfter(true);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public WSrollViewHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public WSrollViewHeader(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs);
    }
    public void setVisiableHeight(int height)
    {
        LayoutParams layoutParams = (LayoutParams) mContainer.getLayoutParams();
        layoutParams.height = height;
        mContainer.setLayoutParams(layoutParams);
    }
    public void scrollY(int position)
    {
        LayoutParams layoutParams = (LayoutParams) mContainer.getLayoutParams();
        int height = mContainer.getMeasuredHeight()+position;
        layoutParams.height = height;
        mContainer.setLayoutParams(layoutParams);
    }
    public int getVisibileHeight()
    {
        return mContainer.getHeight();
    }

    public void resetHeader(int resetHeight) {
        int finalHeight = (header_height-resetHeight);
        mScroller.startScroll(0,getMeasuredHeight(),0,finalHeight,400);
        invalidate();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!first_flag)
        {
            first_flag = true;
            header_height = getMeasuredHeight();
            setVisiableHeight(0);
        }
    }
    private int state=0;
    public int getState(){return this.state;}
    public void setState(int state)
    {
        this.state = state;
        switch (state)
        {
            //刷新状态
            case WScrollView.REFRESHING:
                img_icon.clearAnimation();
                header_hint.setText(getContext().getString(R.string.view_plug_listview_drug_loading));
                img_icon.setVisibility(View.INVISIBLE);
                pro_icon.setVisibility(View.VISIBLE);
            break;
            case WScrollView.SCROLLDOWN_HEADER:
                header_hint.setText(getContext().getString(R.string.view_plug_listview_drug_txt_ready));
                img_icon.setVisibility(View.VISIBLE);
                pro_icon.setVisibility(View.INVISIBLE);
                img_icon.startAnimation(mRotateUpAnim);
                break;
            case WScrollView.SCROLLBACK_HEADER:
                header_hint.setText(getContext().getString(R.string.view_plug_listview_drug_txt_ready));
                img_icon.setVisibility(View.VISIBLE);
                pro_icon.setVisibility(View.INVISIBLE);
                img_icon.startAnimation(mRotateDownAnim);
                break;
        }
    }
}
